import QtQuick 1.1
import com.meego 1.0
import "Modulelib.js" as ModuleUtil
import "CommomUtil.js" as Util
import "const.js" as Const

// 车系详细页面
Page {
    id: module_detail_page

    tools: ModuleToolBar{}

    orientationLock : PageOrientation.LockPortrait

    property int fontSize: 16

    property string serId: "0"

    property string serName: ""

    property int serPhoneNum: 0

    property double minPrice: 0

    property double maxPrice: 0

    property string serImage: ""

    property string serGood: ""

    property string serBad: ""

    TitleBar{
        id: module_detail_title
        anchors.top: module_detail_page.top; anchors.left: module_detail_page.left
        width: module_detail_page.width; height: 71
        titleName: ModuleUtil.current_vehicle_name
    }

    Rectangle{
        id: detail_rect
        width: parent.width; height: 175
        anchors.top: module_detail_title.bottom; anchors.left: parent.left
        color: Const.Color.PageBack
        z: 20
    }

    Rectangle{
        id:vehicles_image
        color: "#b9c6e2"
        border.width: 4; border.color: "#807d7d"
        radius: 8
        anchors.top: detail_rect.top; anchors.topMargin: 25
        anchors.left: detail_rect.left; anchors.leftMargin: 35
        width: 124; height: 94
        z: 20
        Image {
            id: vehicles_img
            source: module_detail_page.serImage
            anchors.centerIn: parent
        }
        MouseArea{
            id: image_mouseArea
            anchors.fill: parent
            onClicked: { // 进入车系图片查看
                ModuleUtil.pic_type = 0;
                Util.openFile("ModulePicList.qml");
            }
        }

        states: [
            State {
                name: 'pressed'; when: image_mouseArea.pressed
                PropertyChanges { target: vehicles_image; focus: true; opacity: 0.5}
            }
        ]
    }

    Text{
        id: photo_num
        text: module_detail_page.serPhoneNum + "张图片"
        width: vehicles_image.width; height: 50
        color: "#8a96aa"
        font{pointSize: 16; bold: true}
        anchors{left: vehicles_image.left; top: vehicles_image.bottom; topMargin: 12}
        horizontalAlignment: Text.AlignHCenter
        z: 20
    }

    Text {
        id: vehicles_name
        text: module_detail_page.serName
        color: "#000000"
        font{pointSize: 22; bold: true}
        anchors{left: vehicles_image.right; top: vehicles_image.top; topMargin: 5; leftMargin: 18}
        height: 40; width: 250
        horizontalAlignment: Text.AlignLeft; verticalAlignment: Text.AlignVCenter
        z: 20
    }

    Text {
        id: vehicles_desc
        text: "厂商指导价:"
        font{pointSize: 12; bold: true}
        anchors{left: vehicles_name.left; top: vehicles_name.bottom; topMargin: 8}
        width: vehicles_name.width; height: 20
        color: "#abb2b3"
        horizontalAlignment: Text.AlignLeft
        z: 20
    }

    Text{
        id: vehicles_price
        text: (module_detail_page.minPrice / 10000) + "~" + (module_detail_page.maxPrice / 10000) + "万"
        font{pointSize: 22; bold: true}
        color: "red"
        anchors{left: vehicles_name.left; top: vehicles_desc.bottom;  topMargin: 5}
        height: 30; width: vehicles_name.width;
        horizontalAlignment: Text.AlignLeft; verticalAlignment: Text.AlignVCenter
        z: 20
    }

    ListModel{
        id: detail_data
    }

    Component{
        id: vehDelegate
        ModuleDetailItem{
            specName: detName
            specEng: detEng
            specPrice: detPrice
            specTrain: detTrain
            specOil: detOil
            specId: detId
            itemType: Type
        }
    }

    Rectangle{
        id: list_view_rect
        width: parent.width; height: 444
        anchors.left: parent.left; anchors.top: detail_rect.bottom
        opacity: 0
    }

    ListView{
        id: list_view
        anchors.fill: list_view_rect
        model: detail_data
        delegate: vehDelegate
        footer: Component{
            Item {
                id: info_dealer
                width: parent.width; height: 86

                Rectangle{
                    id: info_dealer_blank
                    width: parent.width; height: 18
                    anchors.left: parent.left; anchors.top: parent.top
                    opacity: 0
                }

                BorderImage {
                    id: dealar_bt
                    source: "../images/modelslib/dealer_btn.png"
                    width: 451; height: 68
                    anchors.left: parent.left; anchors.top: info_dealer_blank.bottom
                    anchors.leftMargin: 14
                    MouseArea{
                        id: itemMouseArea
                        anchors.fill: dealar_bt
                        onClicked: {
                            ModuleUtil.current_series_id = module_detail_page.serId;
                            ModuleUtil.current_series_name = module_detail_page.serName;
                            Util.openFile("ModuleDealerPage.qml");
                        } //打开车型详细页面
                    }


                    states: [
                        State {
                            name: 'pressed'; when: itemMouseArea.pressed
                            PropertyChanges { target: moreDealerPressed; focus: true; opacity: 0.5}
                        }
                    ]

                }

                Text{
                    id: dealar_desc
                    font{pointSize: 17; bold: true}
                    anchors.centerIn: dealar_bt
                    horizontalAlignment: Text.AlignLeft; verticalAlignment: Text.AlignVCenter
                    color: "#000000"
                    text: "查看经销商"
                }


                Rectangle{
                    id: moreDealerPressed
                    anchors.fill: dealar_bt
                    color: "#000"
                    opacity: 0
                }

            }
        }
    }

    BusyLoading{
        id: loading
        anchors.centerIn: parent
        visible:false
    }

    Component.onCompleted: {
        loading.loading();
        module_detail_page.getData();
    }

    function getData(){
        Util.getXmlStringByUrl(ModuleUtil.getSeriesListUrl(), callback);
    }

    function callback(responseXml){
        if(responseXml !== null){
            var a = responseXml.documentElement;
            module_detail_page.serId = a.childNodes[0].childNodes[0].childNodes[0].nodeValue;
            module_detail_page.serName = a.childNodes[0].childNodes[1].childNodes[0].nodeValue;
            module_detail_page.serPhoneNum = a.childNodes[0].childNodes[2].childNodes[0].nodeValue;
            module_detail_page.minPrice = a.childNodes[0].childNodes[3].childNodes[0].nodeValue;
            module_detail_page.maxPrice = a.childNodes[0].childNodes[4].childNodes[0].nodeValue;
            module_detail_page.serImage = a.childNodes[0].childNodes[5].childNodes[0].nodeValue;
            module_detail_page.serGood = a.childNodes[0].childNodes[6].childNodes[0].nodeValue;
            module_detail_page.serBad = a.childNodes[0].childNodes[7].childNodes[0].nodeValue;
            var backType = "mid"; var specId = 0; var specName = ""; var specSerName = ""; var specPrice = 0;
            var specMinP = 0; var specMaxP = 0; var specEng = ""; var specTrain = ""; var specBody = "";
            var specOil = "";
            for(var i = 1; i < a.childNodes.length; ++i){
                if(a.childNodes[i] !== null && a.childNodes[i].childNodes.length > 9){
                specId = a.childNodes[i].childNodes[0].childNodes[0].nodeValue;
                specName = a.childNodes[i].childNodes[1].childNodes[0].nodeValue;
                specSerName = a.childNodes[i].childNodes[2].childNodes[0].nodeValue;
                specPrice = a.childNodes[i].childNodes[3].childNodes[0].nodeValue;
                specMinP = a.childNodes[i].childNodes[4].childNodes[0].nodeValue;
                    if(a.childNodes[i].childNodes[5] !== null && a.childNodes[i].childNodes[5].childNodes.length > 0)
                specMaxP = a.childNodes[i].childNodes[5].childNodes[0].nodeValue;
                    if(a.childNodes[i].childNodes[6] !== null && a.childNodes[i].childNodes[6].childNodes.length > 0)
                specEng = a.childNodes[i].childNodes[6].childNodes[0].nodeValue;
                    if(a.childNodes[i].childNodes[7] !== null && a.childNodes[i].childNodes[7].childNodes.length > 0)
                specTrain = a.childNodes[i].childNodes[7].childNodes[0].nodeValue;
                    if(a.childNodes[i].childNodes[8] !== null && a.childNodes[i].childNodes[8].childNodes.length > 0)
                specBody = a.childNodes[i].childNodes[8].childNodes[0].nodeValue;
                    if(a.childNodes[i].childNodes[9] !== null && a.childNodes[i].childNodes[9].childNodes.length > 0)
                specOil = a.childNodes[i].childNodes[9].childNodes[0].nodeValue;
                if(i == 1)
                    backType = "top";
                else if(i == (a.childNodes.length - 1))
                    backType = "bottom";
                else
                    backType = "mid";
                detail_data.append({"Type":"item" ,"backType":backType, "detId":specId, "detName":specName,"detEng":specEng,
                                 "detPrice":specPrice, "detMinP":specMinP, "detTrain":specTrain,"detOil":specOil});
                }
            }


        }
        loading.finish();
    }

  }
